[#ftl]
${packagePath}
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.MultiValueMap;

import com.fasterxml.jackson.databind.node.ObjectNode;

/**
  *默认按原有顺序的，自行按需调整顺序，比如查询详情、编辑肯定得数据存在
  */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ${className} extends BaseApplicationTest {
[#-- true是从文件读取，false则是写代码里面 --]
[#assign fileFlag=true]

[#if apiActions??]
[#list apiActions as mdx]
    @Order(${mdx_index+1})
    [#if !mdx.requestVoid]
    @ParameterizedTest
    @MethodSource("${mdx.simpleName}OfReq")//用方法更清晰
    [#else]
    @Test
    [/#if]
    void ${mdx.simpleName}([#if !mdx.requestVoid]String reqstr[/#if]) throws Exception {
        [#if !mdx.requestVoid]
        ObjectNode jsonObj = JsonUtil.parseObject(reqstr);
        [#if (mdx.contentType!'')="application/json"]
        //JSON请求
        [#elseif (mdx.contentType!'')="multipart/form-data"]
        //上传文件请求
        byte[] fileContent = "This is a test file".getBytes();
        MockMultipartFile file = new MockMultipartFile("file", "test.txt", "text/plain", fileContent);
        MultiValueMap<String, String> paramMap=setParam(jsonObj);
        [#else]
        //普通请求
        MultiValueMap<String, String> paramMap=setParam(jsonObj);
        [/#if]
        [/#if]
        String url="${mdx.uris[0]!''}"${UrlPathHadler(mdx,"jsonObj")};
        [#assign method=(mdx.methods[0]!"get")?lower_case  /]
        MvcResult mvcResult=mockMvc.perform(setLoginUserInfo(MockMvcRequestBuilders
             [#if (mdx.contentType!'')="application/json"]
             .${method}(url)
             .contentType(MediaType.APPLICATION_JSON)
             .content(reqstr)))
             [#elseif (mdx.contentType!'')="multipart/form-data"]
             .multipart(url)
             .file(file)
             .params(paramMap)))
             [#elseif method="get"]
             .${method}(url)
             [#if !mdx.requestVoid].queryParams(paramMap)[/#if]))
             [#else] 
             .${method}(url)
             [#if !mdx.requestVoid].params(paramMap)[/#if]))
             [/#if]
             .andExpect(MockMvcResultMatchers.status().isOk())
             .andReturn();
             
             [#if !mdx.responseVoid]
             //如果不是JSON的自行修改
             R<Object> res=JsonUtil.json2Bean(mvcResult.getResponse().getContentAsString(),R.class);
             Assert.assertEquals(R.SUCCESS,res.getCode());
             [/#if]
    }
    [#if !mdx.requestVoid]
    //这样写便于兼容读文件或字符串两种方式
    static List<String> ${mdx.simpleName}OfReq() {
        //测试集，里面已有几条数据就是几次请求，便于测试不同分支情况
        List<String> reqList=new ArrayList<>();
        
        [#if fileFlag]
        List<String> txts = readFileData("${mdx.simpleName}OfReq.json");
        if(txts != null) {
            for(String x:txts) {
                //可以从日志或数据库获取修改
                [#if method != "get" ]
                reqList.add(setUserReqData(JsonUtils.[#if mdx.requestListFlag]parseArray(x)[#else]parseObject(x)[/#if]));
                [#else]
                reqList.add(setUserForGet(JsonUtils.parseObject(x)));
                [/#if]
            }
        }
        [#else][#assign jsonstr=mdx.requestJsonJava]
            [#if mdx.requestListFlag && jsonstr?length>0]
            ArrayNode req = JsonUtil.parseArray("${jsonstr}");
            [#elseif mdx.requestListFlag]
            ArrayNode req = JsonUtil.createArray();
            [#elseif jsonstr?length>0]
            ObjectNode req= JsonUtil.parseObject("${jsonstr}");
            [#else]
            ObjectNode req= JsonUtil.createObject();
            [/#if]
            reqList.add(JsonUtil.toJSONString(req));
        [/#if]
        return reqList;
    }
    [/#if]
    
[/#list]
[/#if]
}

[#function UrlPathHadler mdx dtokey]
[#assign replacestr=""]
[#if mdx.pathParam??]
[#list mdx.pathParam as b]
[#assign replacestr=replacestr+ ".replace(\"{"+b.name+"}\", "+ dtokey +".get(\""+b.name+"\").asText())" ]
[/#list]
[/#if]
[#return replacestr /]
[/#function]